package com.dyzx.hbb.modules.fcxs.controller;

import cn.hutool.core.bean.BeanUtil;
import com.dyzx.hbb.common.config.MyLambdaQueryWrapper;
import com.dyzx.hbb.common.controller.BaseValidController;
import com.dyzx.hbb.modules.fcxs.entity.FcxsFinancial;
import com.dyzx.hbb.modules.fcxs.entity.FcxsOrder;
import com.dyzx.hbb.modules.fcxs.query.FcxsFinancialQuery;
import com.dyzx.hbb.modules.fcxs.service.FcxsFinancialService;
import com.dyzx.hbb.modules.fcxs.service.FcxsOrderService;
import com.dyzx.hbb.modules.fcxs.vo.FcxsFinancialUVO;
import com.dyzx.hbb.modules.fcxs.vo.FcxsFinancialVO;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 收支管理控制器
 * 
 * 功能说明：负责处理财务收支信息的创建、查询、更新和删除等操作
 * 主要职责：
 * - 提供收支信息的CRUD接口
 * - 支持按条件查询收支列表
 * - 处理收支信息添加和编辑时的验证逻辑
 * - 关联订单信息进行业务处理
 * 
 * @author 开发团队
 * @version 1.0.0
 * @since 2023-05-30
 */
@Tag(name = "04.收支管理", description = "收支管理相关接口，包括收支信息的创建、查询、更新和删除等操作")
@RestController
@RequestMapping("/fcxs/financial")
public class FcxsFinancialController extends BaseValidController<
        FcxsFinancialService,
        FcxsFinancial,
        FcxsFinancialQuery,
        FcxsFinancialVO,
        FcxsFinancialUVO
        > {
    @Autowired
    private FcxsFinancialService fcxsFinancialService;

    @Autowired
    private FcxsOrderService fcxsOrderService;

    /**
     * 获取基础服务类
     * @return FcxsFinancialService实例
     */
    @Override
    protected FcxsFinancialService getBaseService() {
        return fcxsFinancialService;
    }


    /**
     * 构建查询条件包装器
     * @param query 查询参数
     * @return 查询条件包装器
     */
    @Override
    protected MyLambdaQueryWrapper<FcxsFinancial> queryWrapper(FcxsFinancialQuery query) {
        MyLambdaQueryWrapper<FcxsFinancial> queryWrapper = new MyLambdaQueryWrapper<>();
        queryWrapper.select(FcxsFinancial::getId)
                .select(FcxsFinancial::getOrderId)
                .fcxsOrderNo(FcxsFinancial::getOrderId).as("order_no")
                .select(FcxsFinancial::getType)
                .select(FcxsFinancial::getCategory)
                .select(FcxsFinancial::getAmt)
                .select(FcxsFinancial::getPayMode)
                .selectNotNull(FcxsFinancial::getImage)
                .imageUrls(FcxsFinancial::getImage).as("image_url")
                .select(FcxsFinancial::getBillType)
                .select(FcxsFinancial::getRemark)
                .select(FcxsFinancial::getStatus)
                .select(FcxsFinancial::getCreateDate)
                .select(FcxsFinancial::getUpdateDate)
                .organName(FcxsFinancial::getOrganId).as("organ_name")
                .userName(FcxsFinancial::getOperatorId).as("operator_name");
        if (query != null) {
            queryWrapper.eq(query.getOrderId() != null, FcxsFinancial::getOrderId, query.getOrderId())
                    .eq(query.getType() != null, FcxsFinancial::getType, query.getType())
                    .eq(query.getCategory() != null, FcxsFinancial::getCategory, query.getCategory())
                    .eq(query.getPayMode() != null, FcxsFinancial::getPayMode, query.getPayMode())
                    .eq(query.getBillType() != null, FcxsFinancial::getBillType, query.getBillType())
                    .eq(StringUtils.isNotBlank(query.getRemark()), FcxsFinancial::getRemark, query.getRemark())

                    .eq(query.getStatus() != null, FcxsFinancial::getStatus, query.getStatus());
        }
        return queryWrapper;
    }

    /**
     * 构建详情查询条件包装器
     * @return 详情查询条件包装器
     */
    @Override
    protected MyLambdaQueryWrapper<FcxsFinancial> queryInfoWrapper() {
        MyLambdaQueryWrapper<FcxsFinancial> queryWrapper = new MyLambdaQueryWrapper<>();
        queryWrapper.select(FcxsFinancial::getId)
                .select(FcxsFinancial::getOrderId)
                .fcxsOrderNo(FcxsFinancial::getOrderId).as("order_no")
                .select(FcxsFinancial::getType)
                .select(FcxsFinancial::getCategory)
                .select(FcxsFinancial::getAmt)
                .select(FcxsFinancial::getPayMode)
                .selectNotNull(FcxsFinancial::getImage)
                .imageUrls(FcxsFinancial::getImage).as("image_url")
                .select(FcxsFinancial::getBillType)
                .select(FcxsFinancial::getRemark)
                .select(FcxsFinancial::getStatus)
                .select(FcxsFinancial::getCreateDate)
                .select(FcxsFinancial::getUpdateDate)
                .organName(FcxsFinancial::getOrganId).as("organ_name")
                .userName(FcxsFinancial::getOperatorId).as("operator_name");
        return queryWrapper;
    }

    /**
     * 添加收支信息时的验证
     * @param entity 收支信息VO
     * @return 验证通过的收支实体
     */
    @Override
    protected FcxsFinancial addValid(FcxsFinancialVO entity) {
        FcxsOrder fcxsOrder = fcxsOrderService.fondByOrderIdOrOrderNo(entity.getOrderId(), entity.getOrderNo());
        if(fcxsOrder == null){
            throw new RuntimeException("找不到订单");
        }
        FcxsFinancial data = new FcxsFinancial();
        BeanUtil.copyProperties(entity, data);
        data.setOrderId(fcxsOrder.getId());
        return data;
    }

    /**
     * 编辑收支信息时的验证
     * @param entity 收支信息更新VO
     * @return 验证通过的收支实体
     */
    @Override
    protected FcxsFinancial editValid(FcxsFinancialUVO entity) {
        if (entity.getOrderId() != null) {
            FcxsFinancial data = new FcxsFinancial();
            FcxsOrder fcxsOrder = fcxsOrderService.fondByOrderId(entity.getOrderId());
            if (fcxsOrder == null) {
                throw new RuntimeException("找不到订单");
            }

            BeanUtil.copyProperties(entity, data);
            data.setOrderId(fcxsOrder.getId());
            return data;
        } else {
            return null;
        }
    }
}
